package com.example.redstarcommerce.cn.dh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.redstarcommerce.cn.dh.common.vo.Result;
import com.example.redstarcommerce.cn.dh.user.mapper.UserMapper;
import com.example.redstarcommerce.cn.dh.user.pojo.User;
import com.example.redstarcommerce.cn.dh.user.service.UserService;
import com.example.redstarcommerce.cn.dh.user.utils.Sha256;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private Sha256 sha256;

    @Override
    public Result<List<User>> getAll() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        List<User> users = userMapper.selectList(queryWrapper);
        if (users.isEmpty()) {
            return Result.fail("用户为空");
        } else {
            return Result.success(users);
        }
    }

    @Override
    public Result<List<User>> getByName(String name, String password) {
        if (name == null || password == null || password.isEmpty() || name.isEmpty()) {
            return Result.fail("未获取到用户名和密码");
        } else {
            password = sha256.SHA256(password);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("name", name);
        queryWrapper.eq("password", password);

        List<User> users = userMapper.selectList(queryWrapper);
        if (users.isEmpty()) {
            return Result.fail("用户或密码错误");
        } else {
            return Result.success(users);
        }
    }

    @Override
    public Result<Boolean> insertUser(String name, String password) {
        if (name == null) {
            return Result.fail("用户为空");
        }
        if (password == null) {
            return Result.fail("密码为空");
        } else {
            password = sha256.SHA256(password);
        }
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        UUID uuid = UUID.randomUUID();
        String uid = uuid.toString();
        User user = new User(uid, name, password, timestamp, timestamp, timestamp);
        boolean b = userMapper.insert(user) > 0;
        if (b) {
            return Result.success(b);
        } else {
            return Result.fail("插入失败");
        }
    }

    @Override
    public Result<Boolean> updateUser(String uid, String name, String password) {
        if (uid == null) {
            return Result.fail("用户为空");
        } else {
            password = sha256.SHA256(password);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {
            user.setName(name);
            user.setPassword(password);
            user.setLasttime(user.getLasttime());
            user.setRecentlytime(new Timestamp(System.currentTimeMillis()));

            boolean b = userMapper.update(user, queryWrapper) > 0;
            return Result.success(b);
        }
        return Result.fail("更新失败");
    }

    @Override
    public Result<Boolean> deleteUser(String uid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        userMapper.delete(queryWrapper);
        return Result.success(true);
    }
}
